const { app, BrowserWindow, ipcMain } = require('electron');
const { addOrUpdateCronTask, loadCronJobs } = require('./cronService.js');
const { loadTaskList, 
    loadTaskSpiderRecords, 
    getTaskConfig, 
    saveTaskInfo, 
    deleteTaskInfo,
    getSpiderContent } = require("./taskService.js");

module.exports = {
    initService: function () {
        ipcMain.on("loadTaskList", async function (event) {
            event.returnValue = await loadTaskList();
        })
        ipcMain.on("loadTaskSpiderRecords", async function (event, params) {
            event.returnValue = await loadTaskSpiderRecords(params);
        })
        ipcMain.on("getTaskConfig", async function (event, taskId) {
            event.returnValue = await getTaskConfig(taskId);
        })
        ipcMain.on("saveTaskInfo", async function (event, data) {
            event.returnValue = await saveTaskInfo(data);
        })
        ipcMain.on("updateCronTask", async function (event, data) {
            var crons = data.spider_crons.split(/[\n]/g);
            event.returnValue = await addOrUpdateCronTask(crons, data);
        })
        ipcMain.on("deleteTask", async function (event, taskId) {
            event.returnValue = await deleteTaskInfo(taskId);
        })
        ipcMain.on("loadCronJobs", async function (event, params) {
            event.returnValue = await loadCronJobs(params);
        })
        ipcMain.on("getSpiderContent", async function (event, spiderId) {
            event.returnValue = await getSpiderContent(spiderId);
        })
    }
}